﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ResimDuzeltme
{
    public partial class Form1 : Form
    {
        string ResimYolu = ConfigurationManager.AppSettings["SiteResimYolu"];
        string cnnString = ConfigurationManager.ConnectionStrings["Veritabani"].ConnectionString;

        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(ResimYolu))
            {
                Directory.CreateDirectory(ResimYolu);
            }
        }

        private void ResimHazirla()
        {
            progressBar1.Minimum = 0;
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT isnull((SELECT     COUNT(*) FROM         mye_ImageData INNER JOIN STOKLAR ON mye_ImageData.RecID_RECno = STOKLAR.sto_RECno WHERE     (mye_ImageData.TableID = 13) AND (mye_ImageData.ImageID = 0)),0)", cn))
                {
                    progressBar1.Maximum = Convert.ToInt32(cmd.ExecuteScalar());
                    cmd.CommandText = "SELECT     STOKLAR.sto_kod,mye_ImageData.Data FROM         mye_ImageData INNER JOIN STOKLAR ON mye_ImageData.RecID_RECno = STOKLAR.sto_RECno WHERE     (mye_ImageData.TableID = 13) AND (mye_ImageData.ImageID = 0)";

                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        byte[] resim = (byte[])rd["Data"];
                        SaveImage(resim, rd.GetString(0));
                        progressBar1.Value += 1;
                    }
                }
                cn.Close();
            }
        }

        void SaveImage(byte[] byteArrayIn, string stokkodu)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image img = Image.FromStream(ms);

            Image byimg = resizeImage(img, new System.Drawing.Size(400, 400));
            Image kcimg = resizeImage(img, new System.Drawing.Size(48, 48));
            byimg.Save(ResimYolu + "\\" + "B" + stokkodu + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            kcimg.Save(ResimYolu + "\\" + "K" + stokkodu + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ResimHazirla();
            MessageBox.Show("Tamamlandı !");
        }
    }
}
